const app = getApp();
const config = require('../../../config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.loginUser == null || app.loginUser.id == null && app.loginUser.mobile == null){
      wx.navigateTo({
        url: '/pages/login/login'
      })
      return;
    }
    if (options.fromPage == 'userindex'){
      this.setData({ fromPage: "userindex" });
    }
    getList(this, app.loginUser.mobile);
  },
  /**
   * 进入详情页
   */
  goDetail : function(e){
    // console.log(e.currentTarget.dataset);
    // console.log(this.data.vipCardArr);
    let _this = this;
    let temparr = new Array();
    if (e.currentTarget.dataset.frompage == 'userindex'){
      if (Array.isArray(_this.data.vipCardArr) && _this.data.vipCardArr.length > 0){
        for (let i = 0; i < _this.data.vipCardArr.length ; i++) {
          if (_this.data.vipCardArr[i].vipNo == e.currentTarget.dataset.cardid){
            temparr = _this.data.vipCardArr[i].list;
            break;
          }
        }
      }
      wx.navigateTo({
        url: 'vipCardBankDetail?vipCardArr=' + JSON.stringify(temparr)
      })
    }else{
      wx.navigateTo({
        url: 'vipCardDetail?cardId=' + e.currentTarget.dataset.cardid
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    getList(this, app.loginUser.mobile);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
  // ,

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
});

/**
 * 查询一卡通信息
 */
function getList(_this, mobile) {

  searchList(_this,mobile, function (msg) {
    // console.log(msg.data.content);
    let arr = new Array();
    if (Object.prototype.toString.call(msg.data.content) === "[object Array]" && msg.data.content.length > 0) {
      _this.data.vipCardArr = msg.data.content;
      for (let i = 0; i < msg.data.content.length; i++) {
        // let temp = msg.data.content[i].accountNo;
        // if(undefined != temp && temp.length > 0){
        //   if(temp.length > 4){
        //     temp = temp.substr(temp.length - 4);
        //   }
        // }else{
        //   temp = "";
        // }
        // arr.push({ vipNo: msg.data.content[i].vipNo, bankName: msg.data.content[i].bankName, accountNo: msg.data.content[i].accountNo, accountName: msg.data.content[i].accountName});
        arr.push({ vipNo: msg.data.content[i].vipNo});
      }
      _this.setData({ showView: true, wuliu: arr });
      // _this.setData({ showView: true, wuliu: msg.data.content });
    }else{
      _this.setData({
        showView: false,
        message: "抱歉，您还没有飞扬一卡通呢！",
        message1: "快到物流公司办理吧！",
        imagePath: "../../images/banka.png"
      });
    }
  });
}

/**
 * 获取一卡通信息
 */
function searchList(_this,mobile, callback) {
  wx.showLoading({ mask: true, title: "加载中" });
  wx.request({
    // url: config.requestUrl + "order/vipCardInfo",
    url: config.requestUrl + "order/userVipCards", 
    method: "POST",
    header: {
      "Content-Type": "application/x-www-form-urlencoded"//;charset=utf-8
    },
    data: { "code": config.tenantCode, "mobile": mobile },
    success: function (msg) {
      callback(msg);
      wx.hideLoading();
    },
    fail: function (msg) {
      // console.log(msg);
      _this.setData({
        showView: false,
        message: "抱歉，没有找到相关数据！",
        imagePath: "../../images/noData.png"
      });
      wx.hideLoading();
    }
  });
}